package httpapi

import (
	"away.wei/minidevpos/db"
	"away.wei/minidevpos/model"
	"github.com/gin-gonic/gin"
)

type ConfigrationApi struct{}

// @Tags Configration
// @Summary 添加
// @Security ApiKeyAuth
// @Param data body db.Configration  true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /configration [post]
func (s *ConfigrationApi) Create(c *gin.Context) {
	var config db.Configration
	_ = c.ShouldBindJSON(&config)
	if err := configrationRep.Create(config); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithMessage("创建成功", c)
	}
}

// @Tags Configration
// @Summary 删除
// @Security ApiKeyAuth
// @Param ids body []int true "编号"
// @Router /configration [delete]
func (s *ConfigrationApi) Delete(c *gin.Context) {
	var ids []int
	c.ShouldBindJSON(&ids)
	if err := configrationRep.Delete(ids); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithMessage("删除成功", c)
	}
}

// @Tags Configration
// @Summary 更新
// @Security ApiKeyAuth
// @Param data body db.Configration  true " "
// @Router /configration [put]
func (s *ConfigrationApi) Update(c *gin.Context) {
	var config db.Configration
	_ = c.ShouldBindJSON(&config)
	if err := configrationRep.Update(config); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithMessage("更新成功", c)
	}
}

// @Tags Configration
// @Summary 根据name查询
// @Security ApiKeyAuth
// @Param name query string true "名称"
// @Router /configration/getbyname [get]
func (s *ConfigrationApi) GetByName(c *gin.Context) {
	name := c.Query("name")
	if sysDictionary, err := configrationRep.GetByName(name); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithDetailed(gin.H{"configration": sysDictionary}, "查询成功", c)
	}
}

// @Tags Configration
// @Summary 分页查询
// @Security ApiKeyAuth
// @Param data query model.ConfigrationSearch true " "
// @Router /configration/getpage [get]
func (s *ConfigrationApi) GetPage(c *gin.Context) {
	var pageInfo model.ConfigrationSearch
	_ = c.ShouldBindQuery(&pageInfo)
	list, total, err := configrationRep.GetPage(pageInfo)

	if err != nil {
		FailWithMessage("获取失败", c)
	} else {
		OkWithDetailed(PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
